package conceptmaniax.hotelapp.weather;
 
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import conceptmaniax.hotelapp.activities.WeatherActivity;
import conceptmaniax.hotelapp.config.Constants;
import android.os.AsyncTask;
 
public class WeatherTask extends AsyncTask<String, Void, String> {
 
	private WeatherActivity activity;

    public WeatherTask(WeatherActivity activity) {
		this.activity = activity;
    }
    
	@Override
	protected String doInBackground(String...params) {
 
		final HttpClient httpclient = new DefaultHttpClient();
		HttpParams httpParams = httpclient.getParams();
		HttpResponse response = null;
		String responseStr = null;
        String url = Constants.WEATHER_API_URL; // added to Constants

        try {
			HttpConnectionParams.setConnectionTimeout(httpParams, Constants.CONNECTION_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, Constants.SOCKET_TIMEOUT);
			ConnManagerParams.setTimeout(httpParams, Constants.CONNECTION_TIMEOUT);
			HttpGet httpGet = new HttpGet(url);
			response = httpclient.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				response.getEntity().writeTo(out);
				out.close();
				responseStr = out.toString();
			}
			
		} catch (Exception e) { 
			e.printStackTrace();
			this.activity.getProgressDialog().dismiss();
		}

		return responseStr;
	}
	
	@Override
	protected void onPostExecute(String result) {
		try {
			final JSONArray dailyArr = new JSONObject(result).getJSONObject("daily").getJSONArray("data");
			WeatherPojo weatherPojo = new WeatherPojo();
		    SimpleDateFormat parseFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.UK);
		    Calendar cal = Calendar.getInstance();      
		    
		    // temperature
		    weatherPojo.setTemperature(WeatherUtils.convertTempWithCelciusSymbol(new JSONObject(result).getJSONObject("currently").getDouble("temperature")));
		    
		    // icon
		    weatherPojo.setToday_icon(new JSONObject(result).getJSONObject("currently").getString("icon").replace("-", "_"));
		    
			// today
		    weatherPojo.setToday_high(WeatherUtils.convertTemp(dailyArr.getJSONObject(0).getDouble("temperatureMax")));
			weatherPojo.setToday_low(WeatherUtils.convertTemp(dailyArr.getJSONObject(0).getDouble("temperatureMin")));
			weatherPojo.setToday_humidity(WeatherUtils.convertHumidity(dailyArr.getJSONObject(0).getDouble("humidity")));
			weatherPojo.setToday_wind(WeatherUtils.convertWind(dailyArr.getJSONObject(0).getDouble("windSpeed")));
			weatherPojo.setToday(parseFormat.format(cal.getTime()));
			// day 1
			cal.add(Calendar.DATE, 1);
			weatherPojo.setDay1_high(WeatherUtils.convertTemp(dailyArr.getJSONObject(1).getDouble("temperatureMax")));
			weatherPojo.setDay1_low(WeatherUtils.convertTemp(dailyArr.getJSONObject(1).getDouble("temperatureMin")));
			weatherPojo.setDay1_humidity(WeatherUtils.convertHumidity(dailyArr.getJSONObject(1).getDouble("humidity")));
			weatherPojo.setDay1_wind(WeatherUtils.convertWind(dailyArr.getJSONObject(1).getDouble("windSpeed")));
			weatherPojo.setDay1_icon(dailyArr.getJSONObject(1).getString("icon").replace("-", "_"));
			weatherPojo.setDay1(parseFormat.format(cal.getTime()));
			// day 2
			cal.add(Calendar.DATE, 1);
			weatherPojo.setDay2_high(WeatherUtils.convertTemp(dailyArr.getJSONObject(2).getDouble("temperatureMax")));
			weatherPojo.setDay2_low(WeatherUtils.convertTemp(dailyArr.getJSONObject(2).getDouble("temperatureMin")));
			weatherPojo.setDay2_humidity(WeatherUtils.convertHumidity(dailyArr.getJSONObject(2).getDouble("humidity")));
			weatherPojo.setDay2_wind(WeatherUtils.convertWind(dailyArr.getJSONObject(2).getDouble("windSpeed")));
			weatherPojo.setDay2_icon(dailyArr.getJSONObject(2).getString("icon").replace("-", "_"));
			weatherPojo.setDay2(parseFormat.format(cal.getTime()));
			// day 3
			cal.add(Calendar.DATE, 1);
			weatherPojo.setDay3_high(WeatherUtils.convertTemp(dailyArr.getJSONObject(3).getDouble("temperatureMax")));
			weatherPojo.setDay3_low(WeatherUtils.convertTemp(dailyArr.getJSONObject(3).getDouble("temperatureMin")));
			weatherPojo.setDay3_humidity(WeatherUtils.convertHumidity(dailyArr.getJSONObject(3).getDouble("humidity")));
			weatherPojo.setDay3_wind(WeatherUtils.convertWind(dailyArr.getJSONObject(3).getDouble("windSpeed")));
			weatherPojo.setDay3_icon(dailyArr.getJSONObject(3).getString("icon").replace("-", "_"));
			weatherPojo.setDay3(parseFormat.format(cal.getTime()));
			
			// dismiss loading bar .. 
			this.activity.getProgressDialog().dismiss();
			
			// set fields to activity
			activity.setFields(weatherPojo);
		} catch (JSONException e) { 
			e.printStackTrace();
			this.activity.getProgressDialog().dismiss();
		} 
	}
}